package virtualstock.request;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

import javax.ejb.EJBException;
import javax.ejb.Stateful;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import virtualstock.entity.Stock;
import virtualstock.exception.InvalidParameterException;
import virtualstock.exception.StockNotFoundException;
import virtualstock.util.StockDetails;

/**
 * Session Bean implementation class StockControllerBean
 */
@Stateful
public class StockControllerBean implements StockController {
	@PersistenceContext
	private EntityManager em;
	private Long stockID = null;

    /**
     * Default constructor. 
     */
    public StockControllerBean() {
        // TODO Auto-generated constructor stub
    }

	@Override
	public Long createStock(StockDetails details)
			throws InvalidParameterException {
		// makes a new stock and enters it into db
		Stock stock = null;
		
		if (details.getStockName() == null) {
			throw new InvalidParameterException("null stockName");
		}
		if (details.getCurrentPrice() == null) {
			throw new InvalidParameterException("null currentPrice");
		}
		
		try {
			stock = new Stock(details.getStockName(), details.getCurrentPrice());
			em.persist(stock);
		} catch (Exception ex) {
			throw new EJBException("createStock: " + ex.getMessage());
		}
		
		return stock.getStockID();
	}

	@Override
	public void removeStock(Long stockID) 
		throws InvalidParameterException, StockNotFoundException {
		// removes stock from db
		if (stockID == null) {
			throw new InvalidParameterException("null stockID");
		}
		
		try {
			Stock stock = em.find(Stock.class, stockID);
			
			if (stock == null) {
				throw new StockNotFoundException();
			}
			else {
				em.remove(stock);
			}
		} catch (Exception ex) {
			throw new EJBException("removeStock: " + ex.getMessage());
		}
	}

	@Override
	public StockDetails getStockDetails(Long stockID)
			throws InvalidParameterException, StockNotFoundException {
		// returns the StockDetails for the specified stock
		StockDetails result = null;
		
		if (stockID == null) {
			throw new InvalidParameterException("null stockID");
		}
		
		try {
			Stock stock = em.find(Stock.class, stockID);
			
			if (stock == null) {
				throw new StockNotFoundException(stockID);
			}
			else {
				result = new StockDetails(
						stock.getStockID(), 
						stock.getStockName(), 
						stock.getCurrentPrice());
			}
		} catch (Exception ex) {
			System.err.println("StockControllerBean.getStockDetails: Caught an exception.");
			ex.printStackTrace();
		}
		
		return result;
	}
	
	private List<StockDetails> copyStockToDetails(Collection<Stock> stocks){
		List<StockDetails> detailsList = new ArrayList<StockDetails>();
		Iterator<Stock> i = stocks.iterator();
		try{
			while(i.hasNext()){
				Stock stock = (Stock)i.next();
				StockDetails details = new StockDetails(stock.getStockID(),
														stock.getStockName(),
														stock.getCurrentPrice());
				detailsList.add(details);
			}			
		}catch(Exception ex){
			throw new EJBException(ex.getMessage());
		}
		return detailsList;
	}
	
	@Override
	public List<StockDetails> getAllStocks(){
		Collection stocks = null;
		try{
			stocks = (List<StockDetails>)em.createNamedQuery("Stock.GetAllStock").getResultList();
		}catch(Exception ex){
			throw new EJBException(ex.getMessage());
		}
	
		if(stocks.isEmpty())
			return null;
		else
			return this.copyStockToDetails(stocks);
	}

}
